<?php
    require_once("core/functions.php");
    core::getResource("Controller.LoginController");

    if (isset($_REQUEST["lEmail"]) && isset($_REQUEST["lPassword"])) {
        $logEmail = $_REQUEST["lEmail"];
        $logPass = $_REQUEST["lPassword"];
        $logController = new LoginController($logEmail, $logPass);
        $result = $logController->tryLogin();
        if ($result == LOGIN_SUCCESS) {
            $_SESSION['personId'] = $logController->getId();
            core::redirect("vwall.php");
        } else {
            echo "ERROR : " . LOGIN_ERROR;
        }
    }


    $action = "viewAll";

    if (isset($_GET["eventId"])) {
        $view = new EventController($_REQUEST["eventId"]);

        if ($view->eventExists()) {
            $action = "specificView";
        }
    }

    if (isset($_GET["startDate"])) {
        $date = Core::sanitize($_GET["startDate"]);
        $data = Event::checkForEvents($date);
        if ($data) {
            $action = "dateView";
        } else {
            $action = "viewAll";
        }
    }
?>
<?php $title = "Event"; ?>
<?php include_once("header.php"); ?>
<div class="centerContent">
<?php
    if ($action == "viewAll") {
        $date = date('Y-m-d');
        $data = EventController::getAllEvents($date);
        if(count($data) == 0){
            echo "No upcoming events";
        }
        for ($i = 0; $i < count($data); $i++) {
        $event = new EventController($data[$i]['eventId']);
?>
            <div class="list" onclick='UTSDK.redirect("viewEvent.php?eventId=<?= $data[$i]['eventId']?>");'>
                <h2>Event: <?= $event->getName() ?></h2>
                <table class="listTable">
                    <tr><td colspan="2">
                            <h4 class="wordWrap"><i><?= $event->getDescription() ?></i></h4>
                        </td></tr>
                    <tr><td><b>Date : </b></td><td><?= $event->getStartAndEndDates() ?></td></tr>
                    <tr><td><b>Location : </b></td><td><?= $event->getLocation() ?></td></tr>
                    <tr>
                        <td colspan="2">
                            <a href="viewEvent.php?eventId=<?= $data[$i]['eventId']?>"><i>View Event</i></a>
                        </td>
                    </tr>
                </table>
            </div>
<?php
        }
    }
    else if ($action == "specificView") {
        ?>
        <button onclick="UTSDK.redirect('viewEvent.php')">Go to Events</button>
        <table>
            <tr><td colspan="2"><h1><?= $view->getName(); ?></h1></td></tr>
            <tr><td colspan="2"><div class="wordWrap"><i><?= $view->getDescription(); ?></i></div></td></tr>
            <tr><td><b>Start and End Date : </b></td><td><?= $view->getStartAndEndDates() ?></td></tr>
            <tr><td><b>Start and End Time : </b></td><td><?= $view->getStartAndEndTimes() ?></td></tr>
            <tr><td><b>Event Location : </b></td><td><?= $view->getLocation() ?></td></tr>
            <tr><td><b>Meeting Location : </b></td><td><?= $view->meetingLocation() ?></td></tr>
            <tr><td><b>Meeting Time : </b></td><td><?= $view->meetingTime() ?></td></tr>
            <tr><td><b>Carpool Location : </b></td><td><?= $view->carPoolLocation() ?></td></tr>
            <tr><td><b>Carpool Time : </b></td><td><?= $view->carPoolTime() ?></td></tr>
            <tr><td colstpan="2"><h2>Activities : </h2></td><td>
            <tr><td colspan="2">
            <?php
            $data = $view->getActivities();
            if(count($data) == 0){
                echo "There are no activities in event";
            }
            for ($i = 0; $i < count($data); $i++) {
            $activity = new ActivityController($data[$i]['activityId']);
                ?>
                <div class="list" onclick='UTSDK.redirect("viewActivity.php?activityId=<?= $data[$i]['activityId'] ?>");'>
                    <h2>Activity: <?= $activity->getTitle() ?></h2>
                    <table class="listTable">
                        <tr>
                            <td colspan="2">
                                <h4 class="wordWrap"><i><?= $activity->getDescription() ?></i></h4>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <a href="viewActivity.php?activityId=<?= $data[$i]['activityId'] ?>"><i>View Activity</i></a>
                            </td>
                        </tr>
                    </table>
                </div>                
            <?php
            }
            ?>

            </td></tr>
        </table>
            
<?php
    } 
    else if ($action == "dateView") {
        $data = EventController::getAllEventsOn($date);
        for ($i = 0; $i < count($data); $i++) {
        $event = new EventController($data[$i]['eventId']);
?>
            <div class="list" onclick='UTSDK.redirect("viewEvent.php?eventId=<?= $data[$i]['eventId']?>");'>
                <h2>Event: <?= $event->getName() ?></h2>
                <table class="listTable">
                    <tr><td colspan="2">
                            <h4 class="wordWrap"><i><?= $event->getDescription() ?></i></h4>
                        </td></tr>
                    <tr><td><b>Date : </b></td><td><?= $event->getStartAndEndDates() ?></td></tr>
                    <tr><td><b>Location : </b></td><td><?= $event->getLocation() ?></td></tr>
                    <tr>
                        <td colspan="2">
                            <a href="viewEvent.php?eventId=<?= $data[$i]['eventId']?>"><i>View Event</i></a>
                        </td>
                    </tr>
                </table>
            </div><?php
        }
    }
?>
</div>
<?php include_once("footer.php"); ?>